﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;

using MVCnExt.Web.UI.Ext;
using System.Text;

[assembly: WebResource("MVCnExt.Web.UI.Ext.Scripts.resources.css.panel.css", "text/css", PerformSubstitution = true)]
namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// A basic tab contentContainer. Tab panels can be used exactly like a standard Ext.Panel for layout purposes, but also have special support for containing child Panels that get automatically converted into tabs.
    /// </summary>
    [Description("A basic tab contentContainer. Tab panels can be used exactly like a standard Ext.Panel for layout purposes, but also have special support for containing child Panels that get automatically converted into tabs.")]
    public class ExtTabPanel : ExtPanel
    {
        private List<ExtComponent> _tbar = new List<ExtComponent>();
        private List<ScriptReference> _references = new List<ScriptReference>();

        public override List<ScriptReference> GetScriptReferences()
        {
            _references.AddRange(base.GetScriptReferences());

            return _references;
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];
            
            desc.Type = "Ext.TabPanel";

            String[] properties = new String[]{
                "resizeTabs", "minTabWidth", "tabWidth", "enableTabScroll", "activeTab", "deferredRender"
            };

            Type component = typeof(ExtTabPanel);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtJsTabPanel.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }            

            //add defaults
            if (tbar.Count != 0)
            {
                // build items list
                StringBuilder itemList = new StringBuilder("[");

                for (int i = 0; i < tbar.Count; i++)
                {
                    // add comma between items
                    if (i != 0)
                    {
                        itemList.Append(",");
#if DEBUG
                        itemList.AppendLine("\t\t\t");
#endif
                    }

                    // get the item script
                    IList<ScriptDescriptor> descriptors = tbar[i].GetScriptDescriptors();

                    // get the items's list of script dependencies
                    List<ScriptReference> references = tbar[i].GetScriptReferences();

                    // make sure the script dependencies are registered
                    _references.AddRange(references);

                    // write the item's config to the list
                    itemList.Append(((ExtScriptControlDescriptor)descriptors[0]).WriteScript(ScriptType.ConfigOnly));
                }

                itemList.Append("]");

                desc.AddPropertyBase("tbar", itemList);
            }

            return list;
        }       

        #region Public Properties

        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<ExtComponent> tbar
        {
            get
            {
                return _tbar;
            }
        }        

        /// <summary>
        /// Minimun size of the region
        /// </summary>
        public int minTabWidth { get; set; }

        /// <summary>
        /// Maximun size of the region
        /// </summary>
        public int tabWidth { get; set; }

        public String activeTab { get; set; }

        public Boolean deferredRender { get; set; }

        public Boolean? resizeTabs { get; set; }

        public Boolean? enableTabScroll { get; set; }

        public override Unit? height { get; set; }

        public override Unit? width { get; set; }

        //public override String id { get; set; }

        #endregion

    }
}
